import re

def use1():
    result=re.match('wangdao','wangdao.cn')
    if result:
        print(result.group())
# use1()
def single():
    ret=re.match('t.o','too')
    print(ret.group())
    ret = re.match('t.o', 'two')
    print(ret.group())
    ret = re.match('.', 'M')
    print(ret.group())
    ret = re.match('[Hh]', 'hello,world')
    print(ret.group())
    ret = re.match('[Hh]', 'Hello,world')
    print(ret.group())
    ret = re.match('[Hh]ello,world', 'hello,world')
    print(ret.group())
    ret = re.match('[0123456789]hello world', '6hello world')
    print(ret.group())
    ret = re.match('[0123456789]Hello world', '4Hello world')
    print(ret.group())
    ret = re.match('[0-37-9]hello world', '0hello world')
    print(ret.group())
    ret = re.match('[Hh]ello world', 'hello world')
    print(ret.group())
    ret = re.match(r'嫦娥\d号', '嫦娥1号发射成功')
    print(ret.group())  # 匹配成功，输出结果
# single()

def more_character():
    ret=re.match('[A-Z][a-z]*','A')
    print(ret.group())
    ret = re.match('[A-Z][a-z]*', 'MnnM')
    print(ret.group())
    ret = re.match('[A-Z][a-z]*', 'Abcdefg')
    print(ret.group())
    names = ['a1','_a','1_a','__a']
    for a in names:
        ret=re.match(r'[a-zA-Z_]\w*',a)
        if ret:
            print(f'{ret.group()}是正确的')
        else:
            print(f'{a}不符合命名规则')
# more_character()
def use_2():
    ret = re.match(r"[1-9]?[0-9]", "7")
    print(ret.group())

    ret = re.match(r"[1-9]?\d", "33")
    print(ret.group())

    ret = re.match(r"[1-9]?\d", "09")
    print(ret.group())
    ret = re.match(r"[0-9]?\d", "09")
    print(ret.group())
    ret = re.match("[a-zA-Z0-9_]{6}", "12a3g45678")
    print(ret.group())
    abc = '12a*3g45678'
    ret = re.match("[a-zA-Z0-9_]{6}", "12a*3g45678")
    if ret:
        print(f'{ret.group()}是正确的')
    else:
        print(f'{abc}是错误的')
    ret = re.match("[a-zA-Z0-9_]{8,20}", "1ad12f23s3*4455ff66cfhhchdhchd")
    print(ret.group())
# use_2()
def use_3():
    email_list = ["xiaoWang@163.com", "xiaoWang@163.comheihei", ".com.xiaowang@qq.com"]
    for email in email_list:
        ret = re.match(r'\w{4,20}@163\.com$', email)
        if ret:
            print(f'{ret.group()}是正确的邮箱{email}')
        else:
            print(f'{email}邮箱地址不正确')
    ret = re.match(r"[1-9]?\d$", "09")#$：确保匹配到字符串的末尾
    if ret:
        print(ret.group())
    else:
        print('不是0-99之间')
# use_3()

def add(x):
    result = x.group()
    return str(int(result) + 100)
def use_4():
    ret = re.search(r"\d+", "阅读次数为 999,点赞888")#search 在字符串中搜索第一个符合正则表达式的匹配项
    print(ret.group())
    ret = re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345")#findall 会返回所有匹配的子串，并以列表形式返回
    print(ret)
    ret = re.sub(r"\d+", '998', "python = 997")
    print(ret)
    ret = re.sub(r"\d+", lambda x: str(int(x.group()) + 100), "python = 997")
    print(ret)
    ret = re.sub(r"\d+", add, "python = 997")
    print(ret)
use_4()